package com.adcc.loadsheet.billing.validator;

import com.adcc.aoc.devLib.common.log.Log;
import com.adcc.loadsheet.billing.entity.contractuser.ContractUserEntity;
import com.adcc.loadsheet.billing.vo.contractuser.ContractUserEntityVO;
import com.google.common.base.Strings;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;

/**
 * Created by zhaoml on 2020/4/13.
 * 合同用户VO校验
 */
@Component
public class ContractUserEntityVOValidator extends ValidatorAdapter<ContractUserEntityVO> {

    @Override
    public void customValidate(ContractUserEntityVO vo, Errors errors) {
        try {
            //用户简称不能为空
            if (Strings.isNullOrEmpty(vo.getUserAbbrev())) {
                errors.reject("用户简称不能为空！");
                return;
            }
            //用户简称长度不能大于20
            if (vo.getUserAbbrev().length() > 40) {
                errors.reject("用户简称长度不能大于40！");
                return;
            }
            //规则全称不能为空
            if (vo.getUserType() == null || vo.getUserType() == 0) {
                errors.reject("请选择用户类型！");
                return;
            }

            //规则简称是否重复
            ContractUserEntity entity = contractUserService.findByUserAbbrev(vo.getUserAbbrev());
            if (entity != null) {
                if (entity.getContractUserId().equals(vo.getContractUserId())){
                    //说明没有修改
                    return;
                }
                //新建、修改时不能重复
                errors.reject("用户简称不能重复！");
                return;
            }
        } catch (Exception e) {
            Log.error(ContractUserEntityVOValidator.class, "customValidate() validate验证出现错误", e);
            errors.reject("数据输入有误！");
        }
    }
}